package edu.must.uh.servlet;

import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import edu.must.uh.bean.DeployQueryResultBean;
import edu.must.uh.impl.DepartmentImpl;
import edu.must.uh.impl.RoleImpl;
import edu.must.uh.util.R;
import edu.must.uh.util.ServletUtils;
import edu.must.uh.util.Tools;
import edu.must.uh.util.ToolsOfString;

@WebServlet("/RoleServlet")
public class RoleServlet extends BaseServlet {
    private static final long serialVersionUID = 12197442526341123L;
    private static Logger logger = Logger.getLogger(RoleServlet.class);  
    RoleImpl roleImpl = new RoleImpl();
    
    public Object getList(HttpServletRequest request, HttpServletResponse response) throws Exception {      
     
    	
    	roleImpl._PageBean = super.pageBean;
        Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
        DeployQueryResultBean resList = roleImpl.getLimitlist(searchcondition, " ORDER BY SortCode ");                 
        return R.succ(resList.getResultList(),resList.getTotal());         
    }
    
public Object geRoleSelect(HttpServletRequest request, HttpServletResponse response) throws Exception {      
        
    	  	
    	List<Map<String, Object>> resList = roleImpl.getRoleSelect();                 
        return R.succ(resList);         
    }
    
    public Object getOneData(HttpServletRequest request, HttpServletResponse response) throws Exception { 
    	String keyValue =ToolsOfString.NulltoEmpty(request.getParameter("ID")); 
    	String sqlString = "SELECT  RoleId, Type,FullName,SortCode FROM base_role Where DeleteMark= 0  ";
    	sqlString = sqlString + " AND RoleId = '"+keyValue+"'";
        Map<String,Object>  map = roleImpl.getMap(sqlString);                 
        return R.succ(map);         
    }
    
    public Object delete(HttpServletRequest request, HttpServletResponse response) throws Exception {      
            
        String keyValue =ToolsOfString.NulltoEmpty(request.getParameter("ID")); 
        Integer resInteger = roleImpl.delete(keyValue);
        return R.succ(resInteger);         
    }
    
  public Object save(HttpServletRequest request, HttpServletResponse response) throws Exception {  
      		
		String sql = "";
		Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
	    String RoleId =ToolsOfString.NulltoEmpty(searchcondition.get("RoleId"));
        String Type =ToolsOfString.NulltoEmpty(searchcondition.get("Type"));  
        String FullName =ToolsOfString.NulltoEmpty(searchcondition.get("FullName"));
        FullName =  java.net.URLDecoder.decode(FullName, "UTF-8");
        String SortCode =ToolsOfString.NulltoEmpty(searchcondition.get("SortCode"));
        if(Tools.notEmpty(RoleId)) {
        	sql = "UPDATE base_role  SET FullName = '"+FullName+"',Type = '"+Type+"',SortCode = '" +SortCode+"'";
        	sql = sql + " WHERE RoleId ='"+RoleId+"'"; 
        }else {
        	RoleId = Tools.getUUID32();
        	sql = "INSERT INTO base_role(RoleId,FullName,Type,SortCode)VALUES";
        	sql = sql + " ('"+RoleId+"','"+FullName+"','"+Type+"','"+SortCode+"')"; 
        }
        Integer resInteger = roleImpl.save(sql);
        if(resInteger == 1) {
        	   return R.succ(); 
        }
        return R.fail();
       
    }
    
    
    
   
}
